//
//  ToolBarView.m
//  XiangMuB
//
//  Created by lanou3g on 16/8/13.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "ToolBarView.h"

#import "AddTagViewController.h"
#import "UIView+FrameView.h"

@interface ToolBarView ()

@property (strong, nonatomic) NSMutableArray *tagLabel;

@property (strong, nonatomic) UIButton *addButton;

@end

@implementation ToolBarView

- (NSMutableArray *)tagLabel{
    
    if (_tagLabel == nil) {
        _tagLabel = [NSMutableArray array];
    }
    
    return _tagLabel;
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupSubviews];
    }
    return self;
}


#pragma mark private method

- (void)addTagButton{
    
//    self.hidden = YES;
    [self endEditing:YES];
    
    AddTagViewController *viewC = [[AddTagViewController alloc] init];
    
    __weak typeof(self) weakself = self;
    [viewC setTagBlock:^(NSArray *tags) {
        
        [weakself createTagLabels:tags];
        
    }];
    
    UIViewController *root = [UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *navigation = (UINavigationController *)root.presentedViewController;
    [navigation pushViewController:viewC animated:YES];
    
    
}

- (void)setupSubviews{
    
    
    CGFloat margin = 10;
    UIButton *thingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    thingButton.frame = CGRectMake(10, 10, 40, 20);
    [thingButton setTitle:@"糗事" forState:UIControlStateNormal];
    thingButton.userInteractionEnabled = NO;
    
    [self addSubview:thingButton];
    
    
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newButton.frame = CGRectMake(thingButton.frame.origin.x + thingButton.frame.size.width + margin, thingButton.frame.origin.y, thingButton.frame.size.width, thingButton.frame.size.height);
    [newButton setTitle:@"趣闻" forState:UIControlStateNormal];
    newButton.userInteractionEnabled = NO;
    
    [self addSubview:newButton];
    
    
    UIButton *addButton = [UIButton buttonWithType:UIButtonTypeCustom];
    addButton.frame = CGRectMake(thingButton.frame.origin.x + 2 * (thingButton.frame.size.width + margin), thingButton.frame.origin.y, thingButton.frame.size.width, thingButton.frame.size.height);
    [addButton setImage:[UIImage imageNamed:@"4.jpg"] forState:UIControlStateNormal];
    [addButton addTarget:self action:@selector(addTagButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:addButton];
    self.addButton = addButton;
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(thingButton.frame) + margin, self.frame.size.width, 1)];
    lineView.backgroundColor = [UIColor whiteColor];
    
    [self addSubview:lineView];
    
    
    
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(margin, CGRectGetMaxY(lineView.frame) + margin, 50, 30);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    cancelButton.backgroundColor = [UIColor grayColor];
    
    [cancelButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:cancelButton];
    
    self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sendButton.frame = CGRectMake(self.frame.size.width - cancelButton.frame.size.width - cancelButton.frame.origin.x, cancelButton.frame.origin.y, cancelButton.frame.size.width, cancelButton.frame.size.height);
    [self.sendButton setTitle:@"发表" forState:UIControlStateNormal];
    self.sendButton.backgroundColor = [UIColor grayColor];
    self.sendButton.userInteractionEnabled = NO;
    [self.sendButton addTarget:self action:@selector(send) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.sendButton];
    
    
    
}

- (void)createTagLabels:(NSArray *)tags{
    
    if ([self.deleate respondsToSelector:@selector(getTagArray:)]) {
        [self.deleate getTagArray:tags];
    }
    
    [self.tagLabel makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.tagLabel removeAllObjects];
    
    for (int i = 0; i < tags.count; i++) {
        
        UILabel *tagLabel = [[UILabel alloc] init];
        [self.tagLabel addObject:tagLabel];
        
        tagLabel.backgroundColor = [UIColor colorWithRed:74/255.0 green:139/255.0 blue:209/255.0 alpha:1.0];
        tagLabel.textAlignment = NSTextAlignmentCenter;
        tagLabel.text = tags[i];
        tagLabel.font = [UIFont systemFontOfSize:14];
        
        [tagLabel sizeToFit];
        tagLabel.width += 2 * 5;
        tagLabel.height = 25;
        tagLabel.textColor = [UIColor whiteColor];
        
        [self addSubview:tagLabel];
        
        
        if (i == 0) {
            
            tagLabel.x = 0;
            tagLabel.y = 0;
            
        } else {
            
            UILabel *lastTagLabel = self.tagLabel[i - 1];
            CGFloat leftWidth = CGRectGetMaxX(lastTagLabel.frame) + 5;
            CGFloat rightWidth = self.width - leftWidth;
            
            if (rightWidth >= tagLabel.width) {
                
                tagLabel.x = leftWidth;
                tagLabel.y = lastTagLabel.y;
                
            } else {
               
                tagLabel.x = 0;
                tagLabel.y = CGRectGetMaxY(lastTagLabel.frame) + 5;
                
            }
            
        }
        
        
    }
    
    UILabel *lastTagLabel = [self.tagLabel lastObject];
    CGFloat leftWidth = CGRectGetMaxX(lastTagLabel.frame) + 5;
    
    if (self.width - leftWidth >= self.addButton.width) {
        
        self.addButton.y = lastTagLabel.y;
        self.addButton.x = leftWidth;
        
    } else {
        
        self.addButton.x = 0;
        self.addButton.y = CGRectGetMaxY(lastTagLabel.frame) + 5;
        
    }
    
}


- (void)dismiss{
    
    if ([self.deleate respondsToSelector:@selector(dismissViewController)]) {
        
        [self.deleate dismissViewController];
    }
    
}

- (void)send{
    
    if ([self.deleate respondsToSelector:@selector(sendMessage)]) {
        
        [self.deleate sendMessage];
        
    }
    
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
